//
//  EffectSelectionView.m
//  EnjoyArkUIXIOS
//
//  Created by Dordly on 2024/2/5.
//

#import "EffectSelectionView.h"
#import "EffectSelectionModel.h"
//#import "ImgTools.h"
#import "PublicHeader.h"

@interface EffectSelectionView ()

@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) NSMutableArray *contentArray;
@property (nonatomic, strong) NSMutableArray *btnContentArray;

@end

@implementation EffectSelectionView

- (instancetype)initWithFrame:(CGRect)frame normalImg:(UIImage *)normalImg {
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.normalImage = normalImg;
        [self createUI];
    }
    return self;
}

- (void)createUI {
    if (self.contentScrollView) {
        [self.contentScrollView removeFromSuperview];
    }
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.contentScrollView.showsVerticalScrollIndicator = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    [self addSubview:self.contentScrollView];
    CGFloat allWidth = 0;
    self.contentArray = [NSMutableArray arrayWithArray:[EffectSelectionModel inputEffectDataArray]];
    self.btnContentArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < self.contentArray.count; i++) {
        EffectSelectionModel * model = self.contentArray[i];
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        selectBtn.layer.masksToBounds = YES;
        selectBtn.layer.cornerRadius = 12;
        [selectBtn setBackgroundColor:[UIColor colorWithRed:200 green:200 blue:200 alpha:1]];
        if (model.effectType < 0) {
            selectBtn.frame = CGRectMake(allWidth, 5, 30, 30);
            [selectBtn setImage:[UIImage imageNamed:model.titleStr] forState:UIControlStateNormal];
            [selectBtn setImage:[UIImage imageNamed:model.titleStr] forState:UIControlStateSelected];
            allWidth += 30;
        } else {
            selectBtn.frame = CGRectMake(allWidth, 5, model.titleStr.length * 25, 30);
            [selectBtn setTitle:model.titleStr forState:UIControlStateNormal];
            [selectBtn setTitle:model.titleStr forState:UIControlStateSelected];
            [selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            selectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            allWidth += (model.titleStr.length * 25);
        }
        allWidth += PUBLIC_SPACE;
        [self.contentScrollView addSubview:selectBtn];
        selectBtn.tag = model.effectType;
        [selectBtn addTarget:self action:@selector(selectEffectClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnContentArray addObject:selectBtn];
    }
    self.contentScrollView.contentSize = CGSizeMake(allWidth, 0);
}

- (void)setNormalImage:(UIImage *)normalImage {
    _normalImage = normalImage;
}

- (void)selectEffectClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.tag >= 0) {
        [sender setBackgroundColor:sender.selected ? [UIColor systemBlueColor] : [UIColor colorWithRed:200 green:200 blue:200 alpha:1]];
    }
    if (self.contentArray.count != self.btnContentArray.count || !_normalImage) {
        return;
    }
    if (sender.tag < 0 && sender.selected) {
        for (UIButton *btn in self.btnContentArray) {
            btn.selected = NO;
            [btn setBackgroundColor:[UIColor colorWithRed:200 green:200 blue:200 alpha:1]];
        }
    }
    self.selectedEffectArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < self.contentArray.count; i++) {
        EffectSelectionModel *model = self.contentArray[i];
        UIButton *btn = self.btnContentArray[i];
        if (!btn.selected) {
            continue;
        }
        NSMutableDictionary *valueDict = [[NSMutableDictionary alloc] init];
        if (model.effectType < 0) {
            valueDict[@"hintTitle"] = @"原图";
            valueDict[@"image"] = _normalImage;
        } else {
            valueDict[@"hintTitle"] = model.titleStr;
//            valueDict[@"image"] = [ImgTools opencvNormalImage:_normalImage processType:model.effectType effectName:model.titleStr];
        }
        [self.selectedEffectArray addObject:valueDict];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(inputResultArray:)]) {
        [self.delegate inputResultArray:[self.selectedEffectArray copy]];
    }
}

- (NSMutableArray *)contentArray {
    if (!_contentArray) {
        _contentArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _contentArray;
}


@end
